package com.ruoyi.tuihuo.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.service.ICangkuService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.kucunpandian.domain.Kucunpandian;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tuihuo.domain.Tuihuo;
import com.ruoyi.tuihuo.service.ITuihuoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 购货退货TestController
 * 
 * @author ruoyi
 * @date 2023-02-06
 */
@Controller
@RequestMapping("/tuihuo/tuihuoTest")
public class TuihuoController extends BaseController
{
    private String prefix = "tuihuo/tuihuoTest";

    @Autowired
    private ITuihuoService tuihuoService;

    @Autowired
    private IKucunpandianService kucunpandianService;

    @Autowired
    private ICangkuService cangkuService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("tuihuo:tuihuoTest:view")
    @GetMapping()
    public String tuihuoTest()
    {
        return prefix + "/tuihuoTest";
    }

    /**
     * 查询购货退货列表
     */
    @RequiresPermissions("tuihuo:tuihuoTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, Tuihuo tuihuo)
    {
        startPage();
        if (!request.getSession().getAttribute("QuanXian").toString() .equals("1") && (tuihuo.getTuihuoCangku() == null || tuihuo.getTuihuoCangku().equals(""))) {
            Tuihuo tuihuo1 = new Tuihuo();
            tuihuo1.setTuihuoCangku(request.getSession().getAttribute("name4user").toString());
            List<Tuihuo> list = tuihuoService.selectTuihuoList(tuihuo1);
            return getDataTable(list);
        }else {
            List<Tuihuo> list = tuihuoService.selectTuihuoList(tuihuo);
            return getDataTable(list);
        }
    }

    /**
     * 导出购货退货列表
     */
    @RequiresPermissions("tuihuo:tuihuoTest:export")
    @Log(title = "购货退货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Tuihuo tuihuo)
    {
        List<Tuihuo> list = tuihuoService.selectTuihuoList(tuihuo);
        ExcelUtil<Tuihuo> util = new ExcelUtil<Tuihuo>(Tuihuo.class);
        return util.exportExcel(list, "购货退货数据");
    }

    /**
     * 新增购货退货
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存购货退货
     */
    @RequiresPermissions("tuihuo:tuihuoTest:add")
    @Log(title = "购货退货", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Tuihuo tuihuo)
    {
        String numstr = tuihuoService.selectGouhuoTuihuoIdByLastOne();
        int num;
        if (numstr == null || numstr.equals("")) {
            num = 1;
        }else {
            num = Integer.parseInt(numstr)+1;
        }
        String b = String.valueOf(num);
        if (String.valueOf(num).length() < 8) {
            int a = 8-String.valueOf(num).length();
            for (int j = 0; j < a; j++) {
                b="0" + b;
            }
        }
        tuihuo.setTuihuoDanhao(b);

        if (tuihuo.getDanjia() != null) {
            if (tuihuo.getShuliang() == null) {
                return toAjax(-1);
            }
            Double jiner= tuihuo.getDanjia()*tuihuo.getShuliang();
            tuihuo.setJiner(jiner);
        }else {
            tuihuo.setJiner(0.0);
        }
        if (tuihuo.getGoodId() == null) {
            return toAjax(-1);
        }
        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByBianHao(tuihuo.getGoodId());
        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(
                tuihuo.getGoodId(), tuihuo.getTuihuoCangku(),shangpinxinxi.getShangpinName());
        if (kucunpandian == null) {
            return toAjax(-1);
        }
        long kuCunNew = kucunpandian.getKuncun()
                -tuihuo.getShuliang().longValue();
        if (kuCunNew < 0) {
            return toAjax(false);
        }
        kucunpandian.setKuncun(kuCunNew);

        kucunpandianService.updateKucunpandian(kucunpandian);

        tuihuo.setGoodName(shangpinxinxi.getShangpinName());

        tuihuo.setGoodGuige(shangpinxinxi.getShangpinGuige());

        return toAjax(tuihuoService.insertTuihuo(tuihuo));
    }

    /**
     * 修改购货退货
     */
    @RequiresPermissions("tuihuo:tuihuoTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Tuihuo tuihuo = tuihuoService.selectTuihuoById(id);
        mmap.put("tuihuo", tuihuo);
        return prefix + "/edit";
    }

    /**
     * 修改保存购货退货
     */
    @RequiresPermissions("tuihuo:tuihuoTest:edit")
    @Log(title = "购货退货", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Tuihuo tuihuo)
    {
        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(
                tuihuo.getGoodId(), tuihuo.getTuihuoCangku(),tuihuo.getGoodName());
        Tuihuo gouhuoTuihuo1 = tuihuoService.selectTuihuoByTuiHuoDanhao(
                tuihuo.getTuihuoDanhao());
        long kuCunNew = kucunpandian.getKuncun()
                -tuihuo.getShuliang().longValue()
                +gouhuoTuihuo1.getShuliang().longValue();
        if (kuCunNew < 0) {
            return toAjax(false);
        }
        kucunpandian.setKuncun(kuCunNew);

        kucunpandianService.updateKucunpandian(kucunpandian);

        Double jiner= tuihuo.getDanjia()*tuihuo.getShuliang();
        tuihuo.setJiner(jiner);
        return toAjax(tuihuoService.updateTuihuo(tuihuo));
    }

    /**
     * 删除购货退货
     */
    @RequiresPermissions("tuihuo:tuihuoTest:remove")
    @Log(title = "购货退货", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if (!ids.contains(",")) {
            kucunSet(ids);
        }else {
            String[] idlist = Convert.toStrArray(ids);
            for (String s:idlist) {
                kucunSet(s);
            }
        }
        return toAjax(tuihuoService.deleteTuihuoByIds(ids));
    }
    private void kucunSet(String ids) {
        Long id = Long.valueOf(ids);
        Tuihuo gouhuoTuihuo = tuihuoService.selectTuihuoById(id);
        Kucunpandian cangkupandian = new Kucunpandian();

        cangkupandian.setCangkuName(gouhuoTuihuo.getTuihuoCangku());
        cangkupandian.setGoodId(gouhuoTuihuo.getGoodId());
        cangkupandian.setGoodName(gouhuoTuihuo.getGoodName());
        cangkupandian.setGoodSpecification(gouhuoTuihuo.getGoodGuige());

        Kucunpandian kucunpandian = kucunpandianService.selectCangkupandianByGoodIdAndCangKuNameAndGoodName(gouhuoTuihuo.getGoodId(), gouhuoTuihuo.getTuihuoCangku(),gouhuoTuihuo.getGoodName());
        cangkupandian.setId(kucunpandian.getId());
        //cangkupandian.setShangpinLeibie(gouhuoTuihuo.getShangpinLeibie());
        cangkupandian.setKuncun(kucunpandian.getKuncun() + gouhuoTuihuo.getShuliang().longValue());

        kucunpandianService.updateKucunpandian(cangkupandian);
    }
}
